package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.AfterSale;
import cn.stylefeng.guns.modular.system.mapper.AfterSaleMapper;
import cn.stylefeng.guns.modular.system.model.params.AfterSaleParam;
import cn.stylefeng.guns.modular.system.model.result.AfterSaleResult;
import  cn.stylefeng.guns.modular.system.service.AfterSaleService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangJun
 * @since 2019-06-25
 */
@Service
public class AfterSaleServiceImpl extends ServiceImpl<AfterSaleMapper, AfterSale> implements AfterSaleService {

    @Override
    public void add(AfterSaleParam param){
        AfterSale entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AfterSaleParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AfterSaleParam param){
        AfterSale oldEntity = getOldEntity(param);
        AfterSale newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AfterSaleResult findBySpec(AfterSaleParam param){
        return null;
    }

    @Override
    public List<AfterSaleResult> findListBySpec(AfterSaleParam param){
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public LayuiPageInfo findPageBySpec(AfterSaleParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public AfterSale getByProcessInstanceId(String processInstanceId) {
        return this.baseMapper.getByProcessInstanceId(processInstanceId);
    }

    private Serializable getKey(AfterSaleParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AfterSale getOldEntity(AfterSaleParam param) {
        return this.getById(getKey(param));
    }

    private AfterSale getEntity(AfterSaleParam param) {
        AfterSale entity = new AfterSale();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
